<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="../function.jsp"%>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑新闻</title>
    <link  href="/images/favicon.ico" rel="shortcut icon" type="image/x-icon">
    <link href="https://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/css/public.css?cache=<%=Math.random()%>">
    <link rel="stylesheet" href="/css/messages_doctor_mes_add.css?cache=<%=Math.random()%>">
    <link rel="stylesheet" href="/css/sweetalert.css?cache=<%=Math.random()%>" >
    <script src="/js/jquery-1.9.1.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <link href="/css/editor/summernote.css" rel="stylesheet">
    <script src="/css/editor/summernote.js"></script>
</head>
<body>
  <%@ include file="../../header.jsp"%>
  <!--内容-->
  <div class="containerone">
  <%@ include file="../../left_menu.jsp"%>
  <!--右边内容-->
  <div class="right_cont">
    <div class="message_right_cont_container" id="edit_news">
    <div class="add_doctor_tittle" v-html="option"></div>
        <div class="add_doctor">
            <span>编辑新闻</span>
            <button class="back_doctor_mes" @click="back()">返回</button>
            </div>
            <div class="news_eidt_title"><span>新闻标题:</span><input class="news_add_tittle" v-model="title" name="title" type="text"  required >
            <%--<p class="news_title_tip">新闻标题不能为空</p>--%>
            <div class="clearfix"></div>
            </div>
            <div class="news_eidt_title"><span>作者:</span><input class="news_add_author"  v-model="author" name="author"  type="text">
            <%--<p class="news_author_tip">作者不能为空</p>--%>
            <div class="clearfix"></div>
            </div>
            <div class="doc_good_at">
            <div class="doc_good_at_tip"><span>新闻简介:</span>(限输入200字符)</div>
            <div class="doc_good_at_textarea">
            <textarea name="summary" id="" style="width:900px;height:100px;"  maxlength="200" class="news_add_summary"  v-model="summary"></textarea>
            </div>
            <%--<p class="news_summary_tip">请输入新闻简介</p>--%>
            </div>
            <div class="doc_good_at">
            <div class="doc_good_at_tip"><span>新闻内容:</span>(限输入2000字符)</div>
            <div style="width:900px;">
            <div id="editor" ></div>
            </div>
            <%--<p class="news_content_tip">请输入新闻内容</p>--%>
            </div>
            <div class="add_doctor_cancel_sure">
            <span class="add_cancel_news" @click="back()">取消</span>
            <button class="add_news_sure add_sure" @click="edit_submit()">确定</button>
            </div>
      <%--医生编辑取消和确定结束--%>
      </div>
    </div>
  </div>
</div>

<script src="/js/sweetalert-dev.js"></script>
<script type="text/javascript">
    new Vue({
    el:"#edit_news",
    data:{
    option:"",
    news_id:"",
    labelId:"",
    title:"",
    author:"",
    summary:"",
    content:"",
    },
    ready:function(){
    var self = this;
    var news = getHrefParms("edit_news");
    self.news_id = news.news_id;
    self.option = news.option;
    $(document).ready(function() {
    $('#editor').summernote({
    height:260,
    lang:'zh-CN',
    callbacks: {
    onImageUpload: function(files) {
    var img = localImg(files[0]);
    }
    }
    });
    });
    function localImg(file) {
    var xmlhttprequest;
    var form = new FormData();
    form.append("file", file);
    if (window.XMLHttpRequest) {
    xmlhttprequest = new XMLHttpRequest();
    if (xmlhttprequest.overrideMimeType) {
    xmlhttprequest.overrideMimeType("text/xml");
     }}
    xmlhttprequest.open("POST", "http://cloud.eeesys.com/pu/upload.php", true);
    xmlhttprequest.onreadystatechange = callback.bind(this);
    xmlhttprequest.send(form);
    function callback() {
    if (xmlhttprequest.readyState == 4 && xmlhttprequest.status == 200) {
    var res = JSON.parse(xmlhttprequest.responseText);
    if (res.errcode == 0) {
    $('#editor').summernote('insertImage', res.img);
    } else {
    swal({
    txt: decodeURI(res.errmsg),
    title: "",
    });
    }
    }
    }
    }
    <%--第一次拿数据--%>
    $.ajax({
    type: 'post',
    url: "edit_news.jsp",
    data: {
    act:"news",
    news_id:self.news_id,
    },
    timeout: 3000,
    dataType: 'json',
    success: function(res) {
    console.log(res);
    if (res.errcode == 0) {
    self.title=res.data.title;
    self.author=res.data.author;
    self.summary=res.data.summary;
    self.content=res.data.content;
    var sHTML = decodeURI(self.content);
    $('.note-editable').html(sHTML) ;
    <%--location.href="news.jsp";--%>
    }
    },
    error: function(error) {
    }
    });
    },
    methods:{
    edit_submit:function(){
    var self = this;
    var sHTML = $('.note-editable').html();
    if(self.title==""){
    swal("新闻标题不能为空");
    return;
    }else if(self.title.length>50){
    swal("新闻标题不能大于50字");
    return;
    }
    if(self.author.length>50){
    swal("作者长度不能大于50字");
    return;
    }
    $.ajax({
    type: 'post',
    url: "edit_news.jsp",
    data: {
    act:"update_news",
    news_id:self.news_id,
    title:self.title,
    author:self.author,
    summary:self.summary,
    content:encodeURI(sHTML.replace(/"/g, "'")),
    },
    timeout: 3000,
    dataType: 'json',
    success: function(res) {
    console.log(res)
    if (res.errcode == 0) {
    location.href="news.jsp";
    }
    },
    error: function(error) {
    }
    });
    },
    back:function(){
    locationHerf("news")
    }
    }
    })

</script>
</body>
</html>
